package course

import (
	"github.com/gin-gonic/gin"
	"github.com/lab-online/pkg/auth"

	"github.com/lab-online/internal/course/domain"
	courseExport "github.com/lab-online/internal/course/export"
	"github.com/lab-online/internal/course/handler"
	"github.com/lab-online/internal/course/infra/repository"
	"github.com/lab-online/internal/course/interface/rest"
)

var (
	NewRepository = repository.New
	NewDomain     = domain.New
	NewExport     = courseExport.New
)

type Route struct {
	rest.CourseHandler
	auth auth.Middleware
}

func NewRoute(authMiddleware auth.Middleware, d domain.CourseDomain) *Route {
	h := handler.New(d)
	return &Route{h, authMiddleware}
}

func (r *Route) Register(router *gin.RouterGroup) {
	authReader := r.auth(auth.RoleAll)
	authWriter := r.auth(auth.RoleAdmin)

	course := router.Group("/courses")
	course.GET("", authReader, handler.SearchCourseValidator, r.SearchCourse)
	course.GET("/:id", authReader, handler.GetCourseValidator, r.GetCourse)
	course.DELETE("/:id", authWriter, handler.RemoveCourseValidator, r.RemoveCourse)
	course.PATCH("/:id", authWriter, handler.UpdateCourseValidator, r.UpdateCourse)
	course.GET("/:id/experiments", authReader, handler.GetExperimentsValidator, r.GetExperiments)

	experiment := router.Group("/experiments")
	experiment.GET("", authReader, handler.SearchExperimentValidator, r.SearchExperiment)
	experiment.GET("/:id", authReader, handler.SearchExperimentValidator, r.SearchExperiment)
	experiment.DELETE("/:id", authWriter, handler.RemoveExperimentValidator, r.RemoveExperiment)
	experiment.PATCH("/:id", authWriter, handler.UpdateExperimentValidator, r.UpdateExperiment)
}
